﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.Net.Mail;
using System.Net;
using System.Configuration;

namespace VietPublisher.Web.BK.Controllers.Customer
{
    public class ContactController : Controller
    {
        //
        // GET: /Contact/


        // Auto send mail for Custommer, when custommer register
        public bool SendMail(string subject, string to, string cc,string body)
        {
            //subject = "Contact form VietPublisher WebSite";
            bool flag = true;
            try
            {
                MailMessage mailMsg = new MailMessage();
                mailMsg.From = new MailAddress("vietPublishercontact@gmail.com", "VietPublisher");
                mailMsg.To.Add(to);
                mailMsg.CC.Add(cc);
                mailMsg.IsBodyHtml = true;
                mailMsg.Subject = subject;
                mailMsg.Body = body;
                mailMsg.Priority = MailPriority.Normal;
                SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                NetworkCredential info = new NetworkCredential("vietPublishercontact@gmail.com", "!@!@mail");
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Credentials = info;
                client.EnableSsl = true;
                client.Send(mailMsg);
            }
            catch (Exception)
            {
                flag = false;
                throw;
            }

            return flag;
        }


        public ActionResult FomSendMail()
        {
            string statusInser = "error";
            string Name = Request.Form["fullname"].ToString();
            string mailuser = Request.Form["mail"].ToString();
            string subject = Request.Form["Subject"].ToString();
            string cc = Request.Form["mailcc"].ToString();
            string body = Request.Form["txtcontent"].ToString();
            //string to = ConfigurationManager.AppSettings["MailTo"];
            //string cc = ConfigurationManager.AppSettings["MailCC"];
           // body = HttpUtility.UrlDecode(body);
            //string to = "sales@beyondchip.com";
           // body = "NguyenDuy....test sendmail";
            string to = "duonggiaduy1990@gmail.com"; // mail mat dinh san de admin nhan dc mail cua khach va hoi am
           // string cc = "duonggiaduy1990@gmail.com";
                       
            bool status = SendMail(subject,to, cc, body);

            if (status)
            {
                statusInser = "success";
            }
            else {
                statusInser = "error";
            }

            return Content(statusInser);
        }


        public ActionResult Index()
        {
            return View();
        }

    }
}
